package com.cpr.core.rabbitmq.springctl.anno.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.URI;

/**
 * @author Chen Panrui
 * created at 2022/3/15 23:44
 */
@Configuration
public class RabbitProducerConfig {

    @Bean
    public ConnectionFactory connectionFactory(){
        // 这个接口下面有几个 实例，可以看下
        return new CachingConnectionFactory(URI.create("amqp://admin:123456@106.13.89.7:5672/%2f"));
    }

    @Bean
    @Autowired
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory){
        return new RabbitTemplate(connectionFactory);
    }

    @Bean
    @Autowired
    public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory){
        return new RabbitAdmin(connectionFactory);
    }

    @Bean
    public Queue queue(){
        return QueueBuilder.nonDurable("queue.anno").build();
    }

    @Bean
    public Exchange exchange(){
        return new FanoutExchange("ex.anno.fanout", false, false);
    }

    @Bean
    @Autowired
    public Binding binding(Queue queue, Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("routing.anno").noargs();
    }
}
